<template>
  <div class="main-card">
    <el-card class="box-card">
      <template #header>
        <span>在线用户</span>
      </template>
      <el-form ref="ruleForm" :model="queryParams" label-width="100px">
        <div class="search-form">
          <el-form-item label="登录ip">
            <el-input v-model="queryParams.ipaddr" clearable />
          </el-form-item>
          <el-form-item label="用户名称">
            <el-input v-model="queryParams.userName" clearable />
          </el-form-item>
          <el-button style="margin-left: 20px" type="primary" @click="search">搜索</el-button>
        </div>
      </el-form>
      <div style="margin:20px 0;">
      </div>
      <div>
        <el-table
          :data="dataList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column
          prop="tokenId"
          label="会话编号"
          width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="userName"
          label="登录名称"
          width="120"
        />
        <el-table-column
          prop="ipaddr"
          label="主机"
          width="120"
        />
        <el-table-column
          prop="loginLocation"
          label="登录地点"
          width="120"
        />
        <el-table-column
          prop="browser"
          label="浏览器"
          width="120"
        />
        <el-table-column
          prop="os"
          label="操作系统"
          width="120"
        />
        <el-table-column
          prop="loginTime"
          label="登录时间"
          width="120"
          :formatter="dateFormat"
        />
        <el-table-column label="操作" width="250">
          <template #default="{row}">
            <el-button v-hasPermi="['monitor:online:forceLogout']" type="primary" icon="el-icon-delete" @click="handleForceLogout(row)">强退</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        v-show="total>0"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
      />
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import { toRefs, reactive, watchEffect, defineComponent } from 'vue'
import { useStore } from 'vuex'
import moment from 'moment'
import { ElMessageBox, ElMessage } from 'element-plus'
import { StateType as OnlineStateType, Online } from '@/store/modules/monitor/online'
import tableList, { BaseQueryType, BaseDataType } from '@/mixins/tableList'

interface QueryParamsType extends BaseQueryType{
  ipaddr: string;
  userName: string;
  beginTime: string | null;
  endTime: string | null;
}

interface DataType extends BaseDataType<Online>{
  queryParams: QueryParamsType;
  isLogDetailShow: boolean;
  dateRange: Array<string>;
}

export default defineComponent({
  name: 'online',
  setup() {
    const store = useStore<{online: OnlineStateType}>()
    const data = reactive<DataType>({
      url: 'online/getList',
      isLogDetailShow: false,
      dateRange: [],
      selectList: [],
      dataList: [],
      total: 0,
      queryParams: {
        ipaddr: '',
        userName: '',
        beginTime: null,
        endTime: null,
        pageNum: 1,
        pageSize: 10
      }
    })
    const {
      dateFormat,
      getList,
      handleSelectionChange,
      handleCurrentChange,
      search
    } = tableList(data)
    search.value()
    watchEffect(() => {
      if (data.dateRange && data.dateRange.length >= 2) {
        data.queryParams.beginTime = moment(data.dateRange[0]).format('YYYY-MM-DD')
        data.queryParams.endTime = moment(data.dateRange[1]).format('YYYY-MM-DD')
      } else {
        data.queryParams.beginTime = null
        data.queryParams.endTime = null
      }
    })
    const handleForceLogout = (row: Online) => {
      ElMessageBox.confirm('是否确认强退名称为"' + row.userName + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return store.dispatch('online/forceLogout', row.tokenId)
      }).then(() => {
        ElMessage({
          message: '删除成功',
          type: 'success'
        })
        getList.value()
      }).catch()
    }
    return {
      dateFormat,
      handleSelectionChange,
      handleCurrentChange,
      search,
      ...toRefs(data),
      handleForceLogout
    }
  }
})
</script>

<style lang="scss">
.el-pagination{
  margin-top: 20px;
}
</style>
